package logic

import (
	"context"
	"encoding/json"
	"time"
	"wechat/models/cache"
	"wechat/models/mysql"

	"wechat/wechatrpc/internal/svc"
	"wechat/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MomentsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMomentsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MomentsListLogic {
	return &MomentsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MomentsListLogic) MomentsList(in *wechatrpc.MomentsListRequest) (*wechatrpc.MomentsListResponse, error) {
	// todo: add your logic here and delete this line

	var moment []*wechatrpc.MomentList

	result, _ := cache.Rdb.Get(l.ctx, "list").Result()

	if result == "" {
		moment = mysql.MomentsList(mysql.DB, moment)
		marshal, _ := json.Marshal(moment)
		cache.Rdb.Set(l.ctx, "list", marshal, time.Minute*5)
	} else {
		json.Unmarshal([]byte(result), &moment)
	}

	return &wechatrpc.MomentsListResponse{
		List: moment,
	}, nil
}
